package tps.video_games;

import java.awt.Color;
import java.util.List;
import java.util.Random;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.GameScene;
import com.uqbar.vainilla.appearances.Circle;
import com.uqbar.vainilla.colissions.CollisionDetector;
import com.uqbar.vainilla.events.constants.Key;

public class Bolita extends GameComponent<GameScene> {
	private static int SPEED = 400;
	private int speed = SPEED;
	private int diameter = 15;
	private double i, j; //vector
	private EstadoBolita estado;
	private boolean modoPegajoso = false;
	

	public Bolita(Paleta paleta) {		
		super(paleta.getX() + paleta.getWidth()/2, paleta.getY() - 20);
		this.setAppearance(new Circle(Color.RED, this.diameter));
		this.pegarAPaleta();
		this.inicializarVectorMovimiento();
	}
	
	//con este constructor se crean las nuevas bolas en multibola
	public Bolita(Paleta paleta,Bolita b) {		
		super(b.getX(), b.getY());
		this.setAppearance(new Circle(Color.RED, this.diameter));
		this.inicializarVectorMovimiento();
		this.estado = new BolitaDespegadaDePaleta(this);
	}
	
	public void inicializarVectorMovimiento(){
		Random random = new Random();
		this.i = random.nextDouble() * 2 - 1;
		this.j = random.nextDouble() * 2 - 1;

		double m = Math.sqrt(this.i * this.i + this.j * this.j);

		this.i = this.i / m;
		this.j = this.j / m;
	}
	
	public void pegarAPaleta(){
		this.estado = new BolitaPegadaAPaleta(this);
	}
	
	public void despegarDePaleta() {
		this.estado = new BolitaDespegadaDePaleta(this);		
	}

	@Override
	public void update(DeltaState deltaState) {		
		this.estado.update(deltaState);		
		super.update(deltaState);
	}
	
	//CHEQUEO DE COLISIONES
	public boolean colisionaConRectangulo(GameComponent<GameScene> rectangulo){
		return CollisionDetector.INSTANCE.collidesCircleAgainstRect
				(this.getX(), this.getY(), this.diameter/2, rectangulo.getX(), rectangulo.getY(), rectangulo.getAppearance().getWidth(),rectangulo.getAppearance().getHeight());
	}
	
	public void chequearColisionConPaleta(){
		Arkanoid arkanoid = (Arkanoid) this.getGame();
		Paleta paleta = arkanoid.getPaleta();
		
		if(this.colisionaConRectangulo(paleta)){
			this.setY(paleta.getY() - this.diameter);
			this.j = -this.j;
			
			//modo pegajoso
			if(this.modoPegajoso)
				this.pegarAPaleta();
		}
		

	}

	public void chequearColisionConLadrillos() {
		Arkanoid arkanoid = (Arkanoid) this.getGame();
		List<Ladrillo> ladrillos = arkanoid.getLadrillos();
		
		Ladrillo ladrilloConElQueChoco = null;
		for(Ladrillo ladrillo : ladrillos){
			if(this.colisionaConRectangulo(ladrillo)){
				if(this.colisionaDeCostado(ladrillo)){
					this.i= -this.i;
				}else{
					this.j = -this.j;
				}	
				ladrillo.recibirGolpe();
				ladrilloConElQueChoco = ladrillo;				
			}
		}
		if(ladrilloConElQueChoco!=null && ladrilloConElQueChoco.esDestruido())
			ladrillos.remove(ladrilloConElQueChoco);			
	}
	
	public boolean colisionaDeCostado(Ladrillo ladrillo){		
		return this.getX() < ladrillo.getX() && this.getY() > ladrillo.getY() && this.getY() < ladrillo.getY() + ladrillo.getHeight()	
				||				
				this.getX() > ladrillo.getX()+ladrillo.getWidth()-this.diameter && this.getY() > ladrillo.getY() && this.getY() < ladrillo.getY() + ladrillo.getHeight();

	}
	
	public void ralentizar() {
		if(this.puedeRalentizar())
			this.speed-=100;		
	}
	
	public boolean puedeRalentizar(){
		return this.speed > 300;
	}
	
	
	//ACCESORS

	public GameComponent<GameScene> getEstado() {
		return this.estado;
	}

	public double getSpeed() {
		return this.speed;
	}
	
	public void setVelocidadNormal(){
		this.speed = SPEED;
	}
	
	public double getI() {
		return i;
	}
	
	public double getJ() {
		return j;
	}
	
	public int getDiameter() {
		return diameter;
	}
	
	public void setI(double i) {
		this.i = i;
	}
	
	public void setJ(double j) {
		this.j = j;
	}
	
	public void setModoPegajoso(boolean modoPegajoso) {
		this.modoPegajoso = modoPegajoso;
	}

}